package Pieces;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import Gamefield.Player;

public class Piece {
	private PiecePlayerEnum _playerColor;
	private char _piecename;
	private int _curPosRow;
	private int _curPosCol;
	private List<MoveDirections> _possibleMoves;
		
	public Piece(PiecePlayerEnum col, char character, int posRow, int posCol) {
		_playerColor = col;
		setPiecename(character);
		_curPosRow = posRow;
		_curPosCol = posCol;
	}
	
	public Piece(Piece copy) {
		this._curPosCol = copy.getPosCol();
		this._curPosRow = copy.getPosRow();
		this._playerColor = copy.getPlayerColor();
		this._piecename = copy.getPiecename();
		
		this._possibleMoves = new LinkedList<MoveDirections>();
		for (MoveDirections posMove : copy.getPossibleMoves()) {
			this._possibleMoves.add(posMove);
		}
	}

	public List<MoveDirections> getPossibleMoves() {
		return _possibleMoves;
	}

	public void setPossibleMoves(List<MoveDirections> possibleMoves) {
		this._possibleMoves = possibleMoves;
	}

	public char getPiecename() {
		return _piecename;
	}

	public void setPiecename(char piecename) {
		String s = piecename+"";
		if (_playerColor == PiecePlayerEnum.White){
			_piecename = s.toUpperCase().charAt(0);
		} else {
			_piecename = s.toLowerCase().charAt(0);
		}
	}
	
	public void capturePiece(){
		String s = _piecename +"";
		if (_playerColor != PiecePlayerEnum.White){ //Black -> White
			_piecename = s.toUpperCase().charAt(0);
			_playerColor = PiecePlayerEnum.White;
		} else {
			_piecename = s.toLowerCase().charAt(0);	// White -> Black
			_playerColor = PiecePlayerEnum.Black;
		}
	}

	public int getPosRow() {
		return _curPosRow;
	}

	public void setPosRow(int PosRow) {
		this._curPosRow = PosRow;
	}

	public int getPosCol() {
		return _curPosCol;
	}

	public void setPosCol(int PosCol) {
		this._curPosCol = PosCol;
	}

	public boolean isWhite() {
		return (_playerColor == PiecePlayerEnum.White);
	}
	
	public PiecePlayerEnum getPlayerColor() {
		return _playerColor;
	}

	public void setPlayerColor(PiecePlayerEnum playerColor) {
		this._playerColor = playerColor;
	}

	public boolean isKing(Player player) {
		if (player.getPlayerCol() == PiecePlayerEnum.White){
			if(_piecename == 'K'){
				return true;
			}
		} else if (player.getPlayerCol() == PiecePlayerEnum.Black){
			if(_piecename == 'k'){
				return true;
			}
		}
		return false;
	}
	
	public String toString(){
		return _piecename+"";
	}

	public void changeDirections() {
		if(_piecename == 'p' || _piecename == 'P'|| _piecename == 'h'|| _piecename == 'H'){
			List<MoveDirections> myMoves = new LinkedList<MoveDirections>();
			for (MoveDirections moves : getPossibleMoves()) {
				myMoves.add(MoveDirections.getOtherDirection(moves));
			}
			setPossibleMoves(myMoves);
		}
	}

	public ArrayList<PieceMovement> getValidMoves() {
		ArrayList<PieceMovement> allValidMoves = new ArrayList<PieceMovement>();
		
		//TODO
	
		return allValidMoves;
	}
}
